/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using System.Qizx.Extensions;
using StringPattern = System.Qizx.Util.StringPattern;

namespace System.Qizx.Util.Basic
{
	
	/// <summary> Expands a root-path and a pattern into a list of files.
	/// <p>Does not support wildcard inside the root path. Only the NAME of
	/// the file is used for matching the pattern.
	/// </summary>
	public class FileCollector
	{
		virtual public System.IO.FileInfo Root
		{
			get
			{
				return root;
			}
			
		}
		virtual public long ByteSize
		{
			get
			{
				return byteSize;
			}
			
		}
		virtual public int Size
		{
			get
			{
				return files.Count;
			}
			
		}
		virtual public StringPattern Filter
		{
			get
			{
				return filter;
			}
			
			set
			{
				this.filter = value;
			}
			
		}
		private System.IO.FileInfo root;
		private bool descent_Renamed_Field;
		private System.Collections.ArrayList files = new System.Collections.ArrayList();
		private long byteSize;
		private StringPattern filter;
		
		public FileCollector(string root, bool descent, StringPattern filter):this(new System.IO.FileInfo(root), descent, filter)
		{
		}
		
		public FileCollector(System.IO.FileInfo root, bool descent, StringPattern filter)
		{
			this.root = root;
			this.descent_Renamed_Field = descent;
			this.filter = filter;
		}
		
		public virtual bool hasDirRoot()
		{
			return System.IO.Directory.Exists(root.FullName);
		}
		
		public virtual void  collect()
		{
			if (System.IO.Directory.Exists(root.FullName))
				descent(root);
			else if (System.IO.File.Exists(root.FullName))
				addFile(root);
		}
		
		private void  addFile(System.IO.FileSystemInfo file)
		{
			files.Add(file);
            byteSize += file.GetLength();
		}
		
		private void  descent(System.IO.FileSystemInfo dir)
		{
			System.IO.FileSystemInfo[] files = dir.GetFiles();
			for (int i = 0; i < files.Length; i++)
			{
				System.IO.FileSystemInfo file = files[i];
				if (System.IO.Directory.Exists(file.FullName))
				{
					if (descent_Renamed_Field)
						descent(file);
				}
				else if (filter == null || filter.matches(file.Name))
					addFile(file);
			}
		}
		
		public virtual System.IO.FileInfo getFile(int index)
		{
			return (System.IO.FileInfo) files[index];
		}
	}
}